//
//  main.m
//  ProjectDemo
//
//  Created by IOS3 on 2018/10/22.
//  Copyright © 2018 IOS3. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

void UncaughtExceptionHandler(NSException *exception);

int main(int argc, char * argv[]) {
    @autoreleasepool {
        @try {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        } @catch (NSException *exception) {
            UncaughtExceptionHandler(exception);
        } @finally {
            
        }
    }
}


void UncaughtExceptionHandler(NSException *exception) {
    NSArray *callStack = [exception callStackSymbols];
    NSString *reason = [exception reason];
    NSString *name = [exception name];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *timeString = [formatter stringFromDate:[NSDate date]];
    NSDictionary *currentErro = @{@"崩溃时间" : timeString,
                                  @"异常名称" : name,
                                  @"异常原因" : reason,
                                  @"堆栈信息" : callStack
                                  };
    DPrint(@"!!!!!!!!!!!!!!!! 程序崩溃, 信息如下: \n%@", currentErro);
    MMKV *mkv = [MMKV defaultMMKV];
    NSArray *errors = [mkv getObjectOfClass:[NSArray class] forKey:KEY_saveException];
    NSMutableArray *marr;
    if (errors) {
        marr = [NSMutableArray arrayWithArray:errors];
        if (errors.count > 9) {
            [marr removeObjectAtIndex:0];
        }
    }
    else {
        marr = [NSMutableArray array];
    }
    [marr addObject:currentErro];
    errors = marr.copy;
    [mkv setObject:errors forKey:KEY_saveException];
    
}
